﻿using Newtonsoft.Json;

namespace D.UtilCore.Dtos
{

    /// <summary>
    /// 返回结果
    /// </summary>
    public static class ResponseUtil
    {
        /// <summary>
        /// 增加、删除、批量删除、修改，返回操作结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static ApiResult<int> NormalResponse(this int result)
        {
            if (result == 0)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, Msg = "暂无信息可操作" };
            }
            else if (result > 0)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.OK, Msg = "操作成功" };
            }
            return new ApiResult<int>() { Code = StatusCodeEnum.Error, Msg = "操作失败" };
        }

        /// <summary>
        /// 返回用户提醒信息
        /// </summary>
        /// <param name="message">提醒内容</param>
        /// <returns></returns>
        public static ApiResult<int> WaringResponse(this string message)
        {
            return new ApiResult<int>() { Code = StatusCodeEnum.Waring, Msg = message };
        }

        /// <summary>
        /// 返回数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ApiResult<T> GetResponse<T>(this T data)
        {
            if (data != null)
            {
                return new ApiResult<T>() { Code = StatusCodeEnum.OK, Data = data };
            }
            return new ApiResult<T>() { Code = StatusCodeEnum.Error, Data = default(T) };
        }
    }

    /// <summary>
    /// 请求反参
    /// </summary>
    public class ApiResult<T> : ApiBaseResult<T>
    {

        /// <summary>生成成功时的应答实例</summary>
        /// <returns>ApiResult 实例</returns>
        public static ApiResult<T> Success()
        {
            return new ApiResult<T> { Code = StatusCodeEnum.OK, Msg = "success" };
        }

        /// <summary>生成成功时的应答实例</summary>
        /// <param name="data">应答数据</param>
        /// <param name="message">应答消息</param>
        /// <returns>ApiResult 实例</returns>
        public static ApiResult<T> Success(T data, string message = "success")
        {
            return new ApiResult<T> { Code = StatusCodeEnum.OK, Data = data, Msg = message };
        }

        /// <summary>生成失败时的应答实例</summary>
        /// <param name="message">应答消息</param>
        /// <returns>ApiResult 实例</returns>
        public static ApiResult<T> Fail(string message)
        {
            return new ApiResult<T> { Code = StatusCodeEnum.Error, Msg = message };
        }

        /// <summary>参数有误</summary>
        public static readonly ApiResult<T> ParamError = new ApiResult<T>() { Code = StatusCodeEnum.Params, Msg = "参数有误" };

        /// <summary>操作成功</summary>
        public static readonly ApiResult<T> OperationSuccessful = new ApiResult<T>() { Code = StatusCodeEnum.OK, Msg = "操作成功" };

        /// <summary>操作失败</summary>
        public static readonly ApiResult<T> OperationFailed = new ApiResult<T>() { Code = StatusCodeEnum.Error, Msg = "操作失败" };

        /// <summary>操作结果</summary>
        /// <param name="isSuccess">是否为操作成功</param>
        /// <returns>ApiResult 实例</returns>
        public static ApiResult<T> Operation(bool isSuccess)
        {
            return isSuccess ? OperationSuccessful : OperationFailed;
        }
    }
}
